package com.crm.controller;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.SysDeptMapper;
import com.crm.model.mpojo.SysDept;
import com.crm.services.SysDeptServices;

/**
 * 权限模块部门Controller
 * 
 * @author 周远星
 *
 */
@Controller

public class SysDeptController {
	@Autowired
	SysDeptServices deptServices;
	@Autowired
	SysDeptMapper sysDeptMapper;

	/**
	 * 添加部门页数据
	 * @return 
	 */
	@PostMapping("/authority/addDeptview")
	@ResponseBody
	public JSONObject addDeptview() {
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("deptAll", deptServices.findAllDept());
		return jsonarray;
	}
	
	/**
	 * 修改部门页数据
	 * @return
	 */
	@PostMapping("/authority/updateDeptview")
	@ResponseBody
	public JSONObject updateDeptview(@RequestParam(name="ID",required=false) Integer id) {
		SysDept sysDept = sysDeptMapper.selectByPrimaryKey(id);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("deptAll", deptServices.findAllDept());
		jsonarray.put("sysDept", sysDept);
		return jsonarray;
	}
	/**
	 * 添加订单
	 * 	
	 * @return
	 */
	@PostMapping("/authority/addDept")
	@ResponseBody
	public String addDept(@RequestParam(name="parentid",required=false) Integer parentid,
			@RequestParam(name="deptName",required=false) String deptName,
			@RequestParam(name="deptSeq",required=false) Integer deptSeq,
			@RequestParam(name="textcommunication",required=false) String textcommunication) {
			SysDept dept=new SysDept();
			dept.setParentid(parentid);
			dept.setName(deptName);
			dept.setSeq(deptSeq);
			dept.setRemark(textcommunication);
		return deptServices.save(dept)+"";
	}
	
	/**
	 * 修改部门
	 * @param id
	 * @param parentid
	 * @param deptName
	 * @param deptSeq
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/authority/updateDept")
	@ResponseBody
	public String updateDept(@RequestParam(name="id",required=false) Integer id,
			@RequestParam(name="parentid",required=false) Integer parentid,
			@RequestParam(name="deptName",required=false) String deptName,
			@RequestParam(name="deptSeq",required=false) Integer deptSeq,
			@RequestParam(name="textcommunication",required=false) String textcommunication) {
		
			SysDept dept =new SysDept();
			dept.setId(id);
			dept.setParentid(parentid);
			dept.setName(deptName);
			dept.setSeq(deptSeq);
			dept.setRemark(textcommunication);
			System.out.println("部门"+dept);
		return deptServices.updateDept(dept)+"";
	}
}
